package com.itxx.allTools.KeyCase.bigDecimalAvg;

import java.math.BigDecimal;

//"今日案例[API]:定义一个String字符串,内容为:91.4 27.23 46.4 38.7 50.2,求这个字符串中数字的平均值
public class Test {
    public static void main(String[] args) {
        String s = "91.4 27.23 46.4 38.7 50.2";
        String[] split = s.split(" ");
        getAvg(split);
    }

    private static void getAvg(String[] split) {
        BigDecimal sum = BigDecimal.ZERO;
        for (int i = 0; i < split.length; i++) {
            BigDecimal bigDecimal = new BigDecimal(split[i]);
            sum = bigDecimal.add(sum);
        }
        System.out.println(sum);
        BigDecimal avg = sum.divide(new BigDecimal(String.valueOf(split.length)), 2, BigDecimal.ROUND_UP);
        System.out.println(avg);
    }
}
